package com.zkkj.demo.service.impl;

import com.zkkj.demo.dao.RedisDao;
import com.zkkj.demo.dto.SysUserDto;
import com.zkkj.demo.entity.LoginUser;
import com.zkkj.demo.exception.SysException;
import com.zkkj.demo.mapper.SysUserMapper;
import com.zkkj.demo.service.LoginService;
import com.zkkj.demo.utils.BeanValidate;
import com.zkkj.demo.utils.FastJsonUtils;
import com.zkkj.demo.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.UUID;

/**
 * author : cyc
 * Date : 2019/12/26
 */

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public String login(LoginUser loginUser) {
        BeanValidate.checkParam(loginUser);
        SysUserDto sysUserDto = sysUserMapper.getSysUserByUsername(loginUser.getUsername());
        if (sysUserDto == null) {
            throw new SysException("用户名不存在");
        }
        if (!Md5Util.MD5Encode(loginUser.getPassword()).equals(sysUserDto.getPassword())) {
            throw new SysException("密码输入错误");
        }
        String key = UUID.randomUUID().toString();
        redisDao.setKey(key, UUID.randomUUID().toString());
        return key;
    }
}
